package com.lej.push.service

import android.content.Context
import android.content.Intent
import com.lej.core.base.app
import com.lej.core.common.LogTags
import com.lej.core.common.PCacheInfo
import com.lej.core.utils.extention.fromJson
import com.lej.core.utils.extention.logI
import com.lej.core.utils.extention.safe
import com.lej.dataservice.club.bean.push.PushPayloadInfo
import com.lej.push.PushUtils
import com.lej.push.notification.NotificationUtils
import com.xiaomi.mipush.sdk.*

class XiaomiMessageReceiver : PushMessageReceiver() {
    private var mRegId: String? = null
    private val mResultCode: Long = -1
    private val mReason: String? = null
    private val mCommand: String? = null
    private var mMessage: String? = null
    private var mTopic: String? = null
    private var mAlias: String? = null
    private var mUserAccount: String? = null
    private var mStartTime: String? = null
    private var mEndTime: String? = null
    override fun onReceivePassThroughMessage(context: Context, message: MiPushMessage) {
        message.getData()
        val pushInfo = mMessage?.fromJson<PushPayloadInfo>()

        pushInfo.toString().logI(LogTags.push)
        pushInfo?.let {
            val intent =
                Intent(app, Class.forName("com.lej.club.ui.PushDispatcherActivity"))
                    .apply { putExtra("payload", mMessage) }
            NotificationUtils.notify(pushInfo.msg.safe(), pushInfo.photoid.hashCode(), intent)
        }
    }

    override fun onNotificationMessageClicked(context: Context, message: MiPushMessage) {
        message.getData()
    }

    override fun onNotificationMessageArrived(context: Context, message: MiPushMessage) {
        message.getData()
    }

    private fun MiPushMessage.getData() {
        mMessage = this.content
        this.topic?.takeIf { it.isNotEmpty() }?.let { mTopic = it }
        this.alias?.takeIf { it.isNotEmpty() }?.let { mAlias = it }
        this.userAccount?.takeIf { it.isNotEmpty() }?.let { mUserAccount = it }
    }

    override fun onCommandResult(context: Context, message: MiPushCommandMessage) {
        val command = message.command
        val arguments = message.commandArguments
        val cmdArg1 = arguments?.getOrNull(0)
        val cmdArg2 = arguments?.getOrNull(1)
        if (message.resultCode == ErrorCode.SUCCESS.toLong()) {
            when (command) {
                MiPushClient.COMMAND_REGISTER -> mRegId = cmdArg1
                MiPushClient.COMMAND_SET_ALIAS -> mAlias = cmdArg1
                MiPushClient.COMMAND_UNSET_ALIAS -> mAlias = cmdArg1
                MiPushClient.COMMAND_SUBSCRIBE_TOPIC -> mTopic = cmdArg1
                MiPushClient.COMMAND_UNSUBSCRIBE_TOPIC -> mTopic = cmdArg1
                MiPushClient.COMMAND_SET_ACCEPT_TIME -> {
                    mStartTime = cmdArg1
                    mEndTime = cmdArg2
                }
            }
        }
    }

    override fun onReceiveRegisterResult(context: Context, message: MiPushCommandMessage) {
        val command = message.command
        val arguments = message.commandArguments
        val cmdArg1 = arguments?.getOrNull(0)
        val cmdArg2 = arguments?.getOrNull(1)
        if (MiPushClient.COMMAND_REGISTER == command) {
            if (message.resultCode == ErrorCode.SUCCESS.toLong()) {
                mRegId = cmdArg1
            }
        }
        "小米推送Token".logI(LogTags.push)
        mRegId?.logI(LogTags.push)
        if (!mRegId.isNullOrEmpty()) {
            PCacheInfo.clientId = mRegId!!
            if (PushUtils.shouldBindClientId) PushUtils.bindCid()
        }
    }
}